//
//  SEGImageChooseViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/4.
//

#import "SEGImageChooseViewModel.h"
NSString * const SEGImageChooseViewCellIndetifier = @"SEGImageChooseViewCellIndetifier";
@interface SEGImageChooseViewModel()
@end

@implementation SEGImageChooseViewModel
-(instancetype)init{
    if (self == [super init]) {
        _maxChoose = @"";
        _lineMax = @"";
        _widgetWidth = [UIScreen mainScreen].bounds.size.width;
        _widgetHeight = kImageChooseViewHeight;
        _imageData = [NSArray array];
    }
    return self;
}
@end

/**************************SEGImageChooseViewLayout************************/
@implementation SEGImageChooseViewLayout
- (instancetype)initWithData:(SEGImageChooseViewModel *)data type:(SEGEnumImageChooseViewType)type{
    if (self == [super init]) {
        _data = data;
        _type = type;
    }
    [self segImageChooseViewLayout];
    return self;
}

-(void)segImageChooseViewLayout{
    //高度固定
    _totalHeight = _data.widgetHeight;
    
    //高度不固定
    //_totalHeight = 0;
    _titleLabelHeight = 0;
    _titleLabelTextLayout = nil;
    _descLabelHeight = 0;
    _descLabelTextLayout = nil;
    
    if (!_data) return;
    _totalHeight = 50;
    [self layout];
}

//数据处理
-(void)layout{
    [self segtitleLabelWithContent:self.data.title fontSize:15 textColor:kColor_1];
    [self segdescLabelWithContent:self.data.desc fontSize:15 textColor:kColor_3];
    
    if(self.type == SEGEnumImageChooseViewType_THREE){
        self.itemSize = CGSizeMake(75, 75);
        NSInteger count =  (int)ceil((self.data.imageData.count + 1) / 4.0);
        _totalHeight += count * (self.itemSize.height + 15);
    }
}

//控件titleLabel文本布局计算
-(void)segtitleLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _titleLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_titleLabelTextLayout)return;
    _titleLabelHeight = [modifier heightForLineCount:_titleLabelTextLayout.rowCount];
}

//控件descLabel文本布局计算
-(void)segdescLabelWithContent:(NSString*)content fontSize:(CGFloat)fontSize textColor:(UIColor*)color{
    if(!content)return;
    UIFont *font = [UIFont fontWithName:@"Heiti SC" size:fontSize];
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:content];
    text.yy_font = font;
    text.yy_color = color;
    
    SEGContentTextLinePositionModifier *modifier = [SEGContentTextLinePositionModifier new];
    modifier.font = font;
    modifier.paddingTop = 1;
    modifier.paddingBottom = 1;
    //modifier.lineHeightMultiple = 1;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(_data.widgetWidth, HUGE);
    container.linePositionModifier = modifier;
    container.truncationType = SEGTextTruncationTypeEnd;// "abcd…"
    
    _descLabelTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    if (!_descLabelTextLayout)return;
    _descLabelHeight = [modifier heightForLineCount:_descLabelTextLayout.rowCount];
}
@end
